/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.action;

import com.jspx.boot.environment.Environment;
import com.jspx.boot.sign.LanguageRes;
import com.jspx.security.utils.EncryptUtil;
import com.jspx.sioc.annotation.Ref;
import com.jspx.txweb.IUserSession;
import com.jspx.txweb.annotation.HttpMethod;
import com.jspx.txweb.annotation.Safety;
import com.jspx.txweb.env.TXWeb;
import com.jspx.txweb.env.TXWebIoc;
import com.jspx.txweb.online.OnlineManager;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.utils.StringUtil;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 11-9-2
 * Time: 下午2:30
 *  挂接方式同步sessionId和cookie,如果是二级域名方式不需要就可以同步
 *
 */
@HttpMethod(caption="同步Session")
public class ClusterSessionAction extends ActionSupport {

    //时间
    private long timeMillis = System.currentTimeMillis();
    //远程ticket
    private String ticket;
    //远程用户名
    private String ticketName;

   //验证成功后跳转到的页面
    private String redirectUrl = StringUtil.empty;

    protected OnlineManager onlineManager;
    @Ref(name = TXWebIoc.onlineManager)
    public void setOnlineManager(OnlineManager onlineManager)
    {
        this.onlineManager = onlineManager;
    }

    public ClusterSessionAction()
    {

    }

    public String getTicket() {
        return ticket;
    }

    @Safety(length = 200)
    public void setTicket(String ticket) {
        this.ticket = ticket;
    }

    public long getTimeMillis() {
        return timeMillis;
    }

    public void setTimeMillis(long timeMillis) {
        this.timeMillis = timeMillis;
    }

    public String getTicketName() {
        return ticketName;
    }

    @Safety(length = 100)
    public void setTicketName(String ticketName) {
        this.ticketName = ticketName;
    }

    public String getRedirectUrl() {
        return redirectUrl;
    }

    @Safety(length = 250)
    public void setRedirectUrl(String redirectUrl) {
        this.redirectUrl = redirectUrl;
    }

    public String execute() throws Exception
    {

        //当前是加密的
        if (StringUtil.isNULL(ticket)) {
            addFieldInfo(Environment.warningInfo,language.getLang(LanguageRes.invalidParameter));
            setActionResult(INPUT);
        }

        //解密
        String sessionId = null;
        try {
            sessionId = new String(onlineManager.getEncrypt().getDecode(EncryptUtil.getBase64Decode(ticket)), Environment.defaultEncode);
        } catch (Exception e) {
            addFieldInfo(Environment.warningInfo,language.getLang(LanguageRes.invalidParameter));
        }

        if (StringUtil.isNULL(ticket)) {
            addFieldInfo(Environment.warningInfo,language.getLang(LanguageRes.invalidParameter));
            setActionResult(INPUT);
        }

        //online user session  必须为分布式缓存,onlineManager相当于验证服务器,密钥也要一样

        if (isGuest())
        {
            setActionResult(INPUT);
        }

        //作为验证
        if (!StringUtil.hasLength(ticketName))
        {
            setActionResult(INPUT);
        }

        IUserSession userSession = onlineManager.getUserSession(sessionId);
        if (EncryptUtil.getMd5(userSession.getName()).equalsIgnoreCase(ticketName))
        {
            //本机保存唯一ticket
            onlineManager.setCookieTicket(request,response,sessionId,3600);
        }
        if (session!=null)
        {
            //必须使用web方式让浏览器放入这个ticket
            session.setAttribute(TXWeb.sessionId,sessionId);
        }
        if (!StringUtil.isNULL(redirectUrl)&&!redirectUrl.endsWith(request.getRequestURI())&&!redirectUrl.endsWith(request.getRequestURL().toString())&&!redirectUrl.contains("login")&&!redirectUrl.contains("/user/index"))
        {
            setActionResult(REDIRECT);
        } else
        {
            setActionResult(SUCCESS);
        }
        return getActionResult();
    }
}